﻿namespace AtfutureCodeMachine
{
    using System;
    using System.Linq;
    using System.IO;
    using System.Net;
    using System.Net.Sockets;
    using System.Text;
    using System.Threading;
    using System.Data;
    using System.Collections;
    using System.Collections.Generic;


    using System.Windows.Forms;
    using NVelocity.App;
    using NVelocity.Runtime;
    using Commons.Collections;
    using NVelocity;
    using NVelocity.Context;
    using System.Configuration;
    using Microsoft.VisualBasic;


    public class ViewTemplate
    {
        static ViewTemplate()
        {
            velocity = new VelocityEngine();
            ExtendedProperties props = new ExtendedProperties(Path.Combine(Application.StartupPath, "nvelocity.properties"));
            props[RuntimeConstants_Fields.FILE_RESOURCE_LOADER_PATH] = WebProcessing.RootPath;
            velocity.Init(props);
        }

        public static void InitViewTemplate(string path)
        {
            velocity_vm = new VelocityEngine();
            ExtendedProperties props = new ExtendedProperties(Path.Combine(Application.StartupPath, "nvelocity.properties"));

            props[RuntimeConstants_Fields.FILE_RESOURCE_LOADER_PATH] =Path.Combine( Application.StartupPath,path);
            velocity_vm.Init(props);
        }

        private static VelocityEngine velocity_vm = null;
        private static VelocityEngine velocity = null;

        public static string ParseVm(string file)
        {
            IContext context = new VelocityContext();

            if (velocity_vm.TemplateExists(file))
            {
                //从文件中读取模板
                Template template = velocity_vm.GetTemplate(file);

                //context.Put("StringUtil", new StringUtil());
                context.Put("Fun", Fun.ParseNew());

                context.Put("Date", DateTime.Now);
                context.Put("ColumnDatas", FormMain.g_list);
                context.Put("GlobalInfo", FormMain.g_globalinfo);
                //合并模板
                StringWriter writer = new StringWriter();
                template.Merge(context, writer);

                return writer.ToString();
            }

            return "没有找到模板！";
        }

        public static string Parse(string filePath,params object[] args)
        {
            IContext context = new VelocityContext();

            if (velocity.TemplateExists(filePath))
            {
                //从文件中读取模板
                Template template = velocity.GetTemplate(filePath);

                WebProcessing.DoAction(filePath, context, args);
                context.Put("PageTitle", "前景互联B/S代码生成工具 AtfutureCodeMachine V4.0");
                context.Put("Fun", Fun.ParseNew());
                context.Put("Date", DateTime.Now);


                StringWriter writer = new StringWriter();
                template.Merge(context, writer);

                return writer.ToString();
            }

            return "没有找到模板！";
        }  
    }
}